package org.csu.leiyun.demo.controller;

import org.csu.leiyun.demo.domain.Account;
import org.csu.leiyun.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/account")
@SessionAttributes("loginAccount")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @GetMapping("/index")
    public String loginForm(){
        return "/account/login";
    }

    @PostMapping("/login")
    public String login(Account account, Model model){
        System.out.println(account.getUsername()+","+account.getPassword());
        Account loginAccount= accountService.login(account);
        if(loginAccount == null){
            model.addAttribute("msg","用户名密码错误");
            return "index";
        }else {
            model.addAttribute("loginAccount",loginAccount);
            model.addAttribute("accountList",accountService.getAllAccountInfo());
            return "main";
        }
    }
}
